var arrList = [{
    id: 1,
    name: '小米',
    pice: 100,
    imgUrl: './images/xiaomi.jpg'
}, {
    id: 2,
    name: '华为',
    pice: 200,
    imgUrl: './images/huawei.jpg'
}, {
    id: 3,
    name: 'opp',
    pice: 300,
    imgUrl: './images/opp.jpg'
}, {
    id: 4,
    name: 'vivo',
    pice: 80,
    imgUrl: './images/vivo.jpg'
}]
const app = new Vue({
    el: '#cart',
    data: {
        shopList: arrList,
        shopData: [],
        showShopList: false
    },
    methods: {
        setShopData(index, types, del, isTab) {
            //获取当前购物车数据
            console.log(index);
            let arr = JSON.parse(window.localStorage.getItem('shopData'))
            let arr1 = [];
            let bool = false;
            let delindex = 0;

            // 判断购物侧数据
            if (arr != null) {
                arr.forEach((value, k) => {
                    //判断是否是购物车点击
                    /* 
                        1、如果点击的是商品，通过判断索引下标添加数量
                        2、如果惦记的是购物车操作，通过id判断改变的数量
                    */
                    let addSHop = false;
                    if (isTab) {
                        addSHop = (index === value.id)
                    } else {
                        addSHop = (this.shopList[index].id === value.id)
                    }

                    //有该商品，数量变更
                    if (addSHop) {
                        bool = true;
                        if (types === "add") {
                            value.num += 1;
                        } else {
                            if (value.num > 1) {
                                value.num -= 1;
                            } else if (del === false) {
                                let bl = window.confirm('再点击就没啦')
                            }
                        }

                    }
                })

                //没有该商品，添加该商品到localstr中
                if (bool == false) {
                    this.shopList[index].num = 1;
                    arr.push(this.shopList[index])
                }
                arr1 = arr;
            } else {
                //localStorage中没有数据
                this.shopList[index].num = 1;
                arr1.push(this.shopList[index])

            }


            if (del) {
                //删除商品
                arr1.forEach((value, k) => {
                    if (value.id == index) {
                        arr1.splice(k, 1)
                    }
                })

            }


            //存储购物侧数据
            window.localStorage.setItem('shopData', JSON.stringify(arr1))
            this.shopData = arr1
        },

        //总计
        getShopPice() {
            let arr = this.shopData;
            let priceNum = 0;
            arr.forEach((value) => {
                priceNum += value.num * value.pice
            })
            return priceNum;
        },

        // 打开购物侧列表----获取以下localstronge存储的信息
        goShopping() {
            let arr = JSON.parse(window.localStorage.getItem('shopData'))
            if (arr) {
                this.shopData = arr
            }
            this.showShopList = true
        },
    }
})